function hideloading() {
    if (barpercent < 100) {
        barpercent = 100;
        bar.style.width = barpercent + '%';
    }
    $('#loading-animation').fadeOut(500);
    //scrollbar fix
    var bottombar = document.getElementsByClassName('bottom-main-nav')[0].parentElement;
    var offsetTop = bottombar.offsetTop;
    var ulstream = document.getElementsByClassName('stream-menu')[0];
    ulstream.style.height = offsetTop - ulstream.offsetTop + 'px';
    $('body script').first().remove();
    $('body').css('overflow', '');
    $('.main-loading-percent').css('display', '');
    if ($('.signal-preview').length == 1 && $('ul.widget-signals').length == 1)
        $('.loading').width($('.signal-preview').offset().left - $('ul.widget-signals').offset().left);
    $('.main-content').height($(window).height());
    if ($('.widget-user_settings, .widget-stream_settings').length > 0)
        $('.widget-user_settings, .widget-stream_settings').height($(window).height());
}

$(document).on('mouseenter', '.main-nav', function() {
    var bottombar = document.getElementsByClassName('bottom-main-nav')[0].parentElement;
    var offsetTop = bottombar.offsetTop;
    var ulstream = document.getElementsByClassName('stream-menu')[0];
    ulstream.style.height = offsetTop - ulstream.offsetTop + 'px';
})

window.onresize = function(event) {
    //scrollbar fix
    var bottombar = document.getElementsByClassName('bottom-main-nav')[0].parentElement;
    var offsetTop = bottombar.offsetTop;
    var ulstream = document.getElementsByClassName('stream-menu')[0];
    ulstream.style.height = offsetTop - ulstream.offsetTop + 'px';
    $('.signal-preview').css('width', document.width - 570 + 'px');
    if ($('.signal-preview').length == 1 && $('ul.widget-signals').length == 1)
        $('.loading').width($('.signal-preview').offset().left - $('ul.widget-signals').offset().left);
    $('.main-content').height($(window).height());
    if ($('.compare-stream-list').length > 0)
        $('.compare-stream-list').css('width', $('.main-content').width() + 20 + 'px');
    if ($('.widget-user_settings, .widget-stream_settings').length > 0)
        $('.widget-user_settings, .widget-stream_settings').height($(window).height());
    var titles = $('.mention-title .action-color-hover');
    if (titles.length > 0) {
        titles.each(function() {
            $(this).width($(this).parent().parent().prev().offset().left - $(this).offset().left);
        });
    }
}

function addClass(el, className) {
    el.className += ' ' + className;
}

function removeClass(el, className) {
    var elClass = ' ' + el.className + ' ';
    while (elClass.indexOf(' ' + className + ' ') != - 1)
        elClass = elClass.replace(' ' + className + ' ', '');
    el.className = elClass;
}

function expand(element) {
    addClass(element.previousSibling.previousSibling, 'hide');
    addClass(element, 'hide');
    removeClass(element.nextSibling.nextSibling, 'hide');
    removeClass(element.nextSibling.nextSibling.nextSibling.nextSibling, 'hide');
}

function unexpand(element) {
    addClass(element.previousSibling.previousSibling, 'hide');
    addClass(element, 'hide');
    removeClass(element.previousSibling.previousSibling.previousSibling.previousSibling, 'hide');
    removeClass(element.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling, 'hide');
}

function ajaxLoad(url) {
    $.post(url,
            function(data, status) {
                $('#page-content').html(data);
            });
    $('#home-fix-style').remove();
}

function preview(url) {
    $('.left-nav').attr('class', 'left-nav hidden');
    if (url != undefined)
        if (url.indexOf('facebook.com') == -1)
            $('#signal-preview').attr('src', url);
        else {
            var fbUserId = url.split('/')[3];
            var fbPostId = url.split('/')[5];
            $('#signal-preview').attr('src', 'http://pulse.buzzmetrics.vn/facebookpreview/' + fbUserId + '/' + fbPostId);
        }
    $('.signal-preview').css('width', $(window).width() - 470 + 'px');
    setTimeout(function() {
        if ($('.signal-preview').length == 1 && $('ul.widget-signals').length == 1)
            $('.loading').width($('.signal-preview').offset().left - $('ul.widget-signals').offset().left);
    }, 100);
    $('.action-nav').css('overflow', 'hidden');
}

function hidePreview() {
    $('.left-nav').attr('class', 'left-nav visible');
    $('#signal-preview').attr('src', "");
    $('.annotable-extra-container').fadeIn();

    setTimeout(function() {
        if ($('.signal-preview').length == 1 && $('ul.widget-signals').length == 1)
            $('.loading').width($(window).width() - $('ul.widget-signals').offset().left);
    }, 100);

    setTimeout(function() {
        $('.action-nav').css('overflow', 'auto');
    }, 500);
}

$(document).keyup(function(e) {
    if (e.keyCode == 27) {
        params = window.location.href.split('#')[1];
        view = params.split('/')[0];
        if (String(view).toUpperCase() === 'STREAM')
            hidePreview();
    }   // esc
});

var __urlRegex = /\((\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;\(\)]*[-A-Z0-9+&@#\/%=~_|])(?=[^>]*(<|$))\)/ig;
var __imgRegex = /\.(?:jpe?g|gif|png)$/i;
function parseURL($string) {
    var exp = __urlRegex;
    return $string.replace(exp, function(match) {
        match = match.substring(1, match.length - 1);
        __imgRegex.lastIndex = 0;
        if (__imgRegex.test(match)) {
            match = match.replace(/&amp;/g, '&');
            if (match.indexOf('&url=') != -1) {
                match = unescape(match.split('&url=')[1]);
            }
            if (match.indexOf('img.youtube.com') == -1)
                return '<img src="' + match + '" class="thumb" />';
            else
                return match;
        }
        else {
            return '<a class="parsed-link hide" href="' + match + '" target="_blank">' + match + '</a>';
        }
    }
    );
}

function showMessage(message) {
    html = '<div class="mozaic-widget notice widget-notification_bar"><div class="notification-icon-holder"><i class="icon-notification-notice"></i></div>\
        <div class="notice bar">\
            <p class="message">' + message + '</p>\
        </div>\
    </div>';

    $('.widget-notifications').html(html);
    $('.widget-notifications').css('top', '0');
    setTimeout(function() {
        $('.widget-notifications').css('top', '-53px');
    }, 2000);
}